package org.one.stone.soup.grfxML.plugin.widget;

import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

import org.one.stone.soup.grfxML.DataSize;
import org.one.stone.soup.grfxML.DataText;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.plugin.SimplePlugin;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.stringhelper.StringGenerator;

public class Clock extends SimplePlugin {

	DataText time = new DataText();

	DataSize rHour = new DataSize();
	DataSize rHoursLeft = new DataSize();
	DataSize rMinute = new DataSize();
	DataSize rSecond = new DataSize();
	DataSize rMillisecond = new DataSize();

	int lastHour=0;
	int lastMinute=0;
	int lastSecond=0;
	int lastMilli=0;
public Clock(GrfxMLEngine engine) {
	super(engine);
}
/**
 * initialize method comment.
 */
public void initialize() {}
/**
 * process method comment.
 */
public void process() {

 try{
	Calendar now = Calendar.getInstance(TimeZone.getDefault(),Locale.UK);

	int hour = now.get( Calendar.HOUR );
	hour = hour%13;

	int hour24 = now.get( Calendar.HOUR_OF_DAY );

	int minute = now.get( Calendar.MINUTE );
	int second = now.get( Calendar.SECOND );
	int millis = now.get( Calendar.MILLISECOND );

	rHour.setValue( (hour*30)+(minute/2),this );
	rHoursLeft.setValue( 360-rHour.getValue(),this );
	lastHour = hour;

	rMinute.setValue( (minute*6),this );
	lastMinute = minute;

	if(lastSecond!=second)
	{
		rSecond.setValue( (second*6),this );
		lastSecond = second;
	}

	if(lastMilli!=millis)
	{
		rMillisecond.setValue( (millis*36/100),this );
		lastMilli = millis;
	}

	if(time.size()>0)
	{
		time.get(0).setValue(
				"Time: "+
				StringGenerator.pad(""+hour24,2,'0')+hour24+
				":"+
				StringGenerator.pad(""+minute,2,'0')+minute+
				":"+
				StringGenerator.pad(""+second,2,'0')+second+
				"."+
				millis
			,this);
	}
	if(time.size()>1)
	{
		time.get(1).setValue(
				now.get(Calendar.DAY_OF_MONTH)+"/"+
				now.get(Calendar.MONTH)+"/"+
				now.get(Calendar.YEAR)
			,this);
	}
 }
 catch(LockException le){}//dummy catch
}
public void register(DataLinker store) {

	registerFieldAs(time,"Clock.time",store);
	registerFieldAs(rHour,"Clock.hours",store);
	registerFieldAs(rHoursLeft,"Clock.hoursLeft",store);
	registerFieldAs(rMinute,"Clock.minutes",store);
	registerFieldAs(rSecond,"Clock.seconds",store);
	registerFieldAs(rMillisecond,"Clock.milliseconds",store);

}
/**
 * replace method comment.
 */
public void replace(Field oldObj,Field newObj) {

	time = grfxMLCaster.replace(time,oldObj,newObj);
	rHour = grfxMLCaster.replace(rHour,oldObj,newObj);
	rHoursLeft = grfxMLCaster.replace(rHoursLeft,oldObj,newObj);
	rMinute = grfxMLCaster.replace(rMinute,oldObj,newObj);
	rSecond = grfxMLCaster.replace(rSecond,oldObj,newObj);
	rMillisecond = grfxMLCaster.replace(rMillisecond,oldObj,newObj);
}
/**
 * stop method comment.
 */
public void stop() {}
}
